<?php
class profiles
{
	private static $instance = null;
	
	private $userprofile;
	private $profileoption;
	
	public function __construct()
	{

		$db = db::getDbIstance();
		
		
		if(isset($_SESSION['username']) and !isset($_SESSION['userid']))
		{
			if(!$db->query("INSERT INTO user SET username = '{$_SESSION['username']}', profileid = '".config::getconfig('_default_profile')."', lastactivity = ".time()." "))
				echo $db->error;
			else
				$_SESSION['userid'] = $db->insert_id;
		}
		elseif(isset($_SESSION['username']) and isset($_SESSION['userid']))
		{
			$UserRes = $db->query("SELECT * FROM user WHERE username = '{$_SESSION['username']}'");
			$this->userprofile = $db->fetch_array($UserRes);
			if(!$db->query("UPDATE user SET lastactivity = ".time()." WHERE username = '{$_SESSION['username']}'"))
				echo $db->error;
		}
		
		if(!$db->query("SELECT profileoptionname, profileoptionvalue FROM profileoption WHERE profileid = '{$this->userprofile['profileid']}'"))
			echo $db->error;
		while(list($optionname, $optionvalue) = $db->fetch_row())
		{
			$this->profileoption[$optionname] = $optionvalue;
		}
	}
	
	static public function getProfilesIstance()
	{
		if(self::$instance == null)
		{   
	    	$c = __CLASS__;
	     	self::$instance = new $c;
		}
	     
	    return self::$instance;
	}
	
	
	public function permission($profileoption)
	{
		if($this->profileoption[$profileoption])
			return TRUE;
		else
			return FALSE;
	}
	
	public function getUserProfileDesc()
	{
		$db = db::getDbIstance();
		$db->query("SELECT profiledesc FROM profile WHERE profileid = '{$this->userprofile['profileid']}'");
		list($profiledesc) = $db->fetch_row();
		
		return $profiledesc;
		
	}

}
?>